<?php
class ArticleCat extends My_Model {
	var	$_table			= 'articlecats';

    function __construct()
    {
        parent::My_Model();
    }

	function doList($parent_id=null){
		static $res = NULL;
		if ($res === NULL)
		{
			$dbPrefix = $this->db->dbprefix;
			$table = $this->db->dbprefix.$this->_table;
			$this->db->dbprefix = '';
			$this->db->select("$table.*,COUNT(s.id) AS has_children");
			$this->db->from($table);
			$this->db->join("$table AS s", "s.parent_id={$table}.id", 'left');
			if($parent_id!=null) $this->db->where(array("{$table}.parent_id"=>$parent_id));
			$this->db->group_by("$table.id");
			$this->db->order_by('id,sort_order ASC');
			//$this->db->cache_on();
			$query = $this->db->get();
			//$this->db->cache_off();
			$this->db->dbprefix = $dbPrefix;
			foreach ($query->result_array() as $row)
			{
			  $res[] = $row;
			}
		}
		return $res;
	}
}